package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.entitiy.TbSpecGroup;
import com.leyou.item.mapper.TbSpecGroupMapper;
import com.leyou.item.service.TbSpecGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 服务实现类
 * </p>
 *
 * @author HM
 * @since 2021-06-16
 */
@Service
public class TbSpecGroupServiceImpl extends ServiceImpl<TbSpecGroupMapper, TbSpecGroup> implements TbSpecGroupService {
    @Autowired
    private TbSpecGroupService specservice;

    @Override
    public List<SpecGroupDTO> findSpecGroupByCategoryId(Long id) {
        QueryWrapper<TbSpecGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbSpecGroup::getCid, id);
        List<TbSpecGroup> specGroupList = specservice.list(queryWrapper);
        if (CollectionUtils.isEmpty(specGroupList)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
    }

    @Override
    public void add(TbSpecGroup tbSpecGroup) {
        baseMapper.insert(tbSpecGroup);
    }

    @Override
    public void updat(TbSpecGroup tbSpecGroup) {
        baseMapper.updateById(tbSpecGroup);
    }

    @Override
    public void delete(Long id) {
        if (id == 0 || id == null){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        this.removeById(id);
    }
}
